package com.haoze.nurse.service.impl;

import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.common.utils.DateUtil;
import com.haoze.common.utils.UUIDUtil;
import com.haoze.nurse.dao.*;
import com.haoze.nurse.dto.Element;
import com.haoze.nurse.enumeration.InOutValueTypeEnum;
import com.haoze.nurse.model.*;
import com.haoze.nurse.service.EmrNurseStatisticsService;
import com.haoze.nurse.vo.EmrFileData;
import com.haoze.nurse.vo.NurseIntokeStokeTime;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

@Service
public class EmrNurseStatisticsServiceImpl implements EmrNurseStatisticsService {

    private static final Logger logger = LoggerFactory.getLogger(NurseStatisticsServiceImpl.class);
    @Resource
    NurseOrdersMapper nurseOrdersMapper;

    @Resource
    FileIndexMapper fileIndexMapper;

    @Resource
    EmrTemplateMapper emrTemplateMapper;

    @Resource
    NurseFileMapper nurseFileMapper;

    @Resource
    EmrStatisticsMapper emrStatisticsMapper;

    @Resource
    FileIndexDataMapper fileIndexDataMapper;

    @Resource
    NurseStatisticsMapper nurseStatisticsMapper;

    @Resource
    FileIndexDataElementMapper fileIndexDataElementMapper;

    @Resource
    NurseStatisticsDetailMapper nurseStatisticsDetailMapper;

    @Resource
    FileIndexEntityMapper fileIndexEntityMapper;

    @Resource
    FileIndexDataEntityMapper fileIndexDataEntityMapper;

    @Resource
    NursePatientMapper nursePatientMapper;

    @Override
    public void updateAllStatistics(List<Map<String,String>> list, String inpatNum, Date nodeTime, String fileIndexDataId) {
        for (int i = 0;i < list.size();i++){
            long t0 = System.currentTimeMillis();
            if("0".equals(list.get(i).get("statisticsType"))){
                inputOutputStatistics(inpatNum, list.get(i).get("endTime"),list.get(i).get("statisticsType"), list.get(i).get("statisticsId"),list.get(i).get("fileIndexDataId"),list.get(i).get("tplId"));
            }else {
                inputOutputStatistics(inpatNum, list.get(i).get("endTime"), list.get(i).get("statisticsType"), list.get(i).get("statisticsId"), fileIndexDataId, list.get(i).get("tplId"));
            }
            long t1 = System.currentTimeMillis();
            logger.info("{}-类型{}统计耗时： {} ms", list.get(i).get("endTime"),list.get(i).get("statisticsType"), t1 - t0);
        }
    }

    @Override
    public void addAllstatistics(String inpatNum,String tplId, Date nodeTime, String fileIndexDataId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date nodeDate = null;
        try {
            nodeDate = sdf.parse(sdf.format(nodeTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String varCode = fileIndexDataMapper.queryVarCodeByFileIndexDataId(fileIndexDataId);
        String statisticsType = null;
        if("twosum".equals(varCode)){
            statisticsType = "1";
        }else if("eigsum".equals(varCode)){
            statisticsType = "2";
        }else if("eigout".equals(varCode)){
            statisticsType = "3";
        }else if("tfout".equals(varCode)){
            statisticsType = "4";
        }else if("eigurine".equals(varCode)){
            statisticsType = "5";
        }else if("tfurine".equals(varCode)){
            statisticsType = "6";
        }else if("eigrinse".equals(varCode)){
            statisticsType = "7";
        }else if("tfrinse".equals(varCode)){
            statisticsType = "8";
        }

        long t0 = System.currentTimeMillis();
        NurseIntokeStokeTime nurseIntokeStokeTime = computeInputOutputTime(inpatNum, nodeDate, statisticsType);
        long t1 = System.currentTimeMillis();
        logger.info("获取统计时间范围耗时： {} ms", t1 - t0);

        NurseStatisticsEntity nurseStatisticsEntity = new NurseStatisticsEntity();
        nurseStatisticsEntity.setStatisticsId(UUIDUtil.randomString());
        nurseStatisticsEntity.setDataVersion(1);
        nurseStatisticsEntity.setDuration(nurseIntokeStokeTime.getdValue());
        nurseStatisticsEntity.setFlag("0");
        nurseStatisticsEntity.setFirstStatistics(nurseIntokeStokeTime.getFirst());
        nurseStatisticsEntity.setHospitalId("");
        nurseStatisticsEntity.setStatisticsType(statisticsType);
        nurseStatisticsEntity.setInpatNum(inpatNum);
        nurseStatisticsEntity.setStartTime(sdf.format(nurseIntokeStokeTime.getBeginDate()));
        nurseStatisticsEntity.setEndTime(sdf.format(nurseIntokeStokeTime.getEndDate()));
        nurseStatisticsEntity.setFileIndexdataId(fileIndexDataId);
        nurseStatisticsEntity.setTplId(tplId);


        //获取统计明细表中的相关数据返回list
        List<Map<String, String>> list = fileIndexDataMapper.queryStatisticsDetailDatas(inpatNum, tplId, nurseIntokeStokeTime.getBeginDate(), nurseIntokeStokeTime.getEndDate());
        //将明细表中的数据拼接为字符串放入statistics表中的statisticsText字段
        String statisticsText = countStatistics(nurseStatisticsEntity.getStatisticsId(), inpatNum, list, nurseIntokeStokeTime, statisticsType);

        nurseStatisticsEntity.setStatisticsText(statisticsText);
        // 插入新的统计记录
        nurseStatisticsMapper.insert(nurseStatisticsEntity);
    }


    @Override
    @Transactional
    public Result getInOutSummary(String inpatNum, String startTime,String endTime){
        String varCode = "crlzjmb";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String startTimeStr = null;
        String endTimeStr = null;
        try {
            startTimeStr = sdf.format(sdf.parse(startTime));
            endTimeStr = sdf.format(sdf.parse(endTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //根据前台传递的结束时间获取包含结束时间的最近的一条医嘱记录
        Map<String, String> currPatientOrder = emrStatisticsMapper.queryOrdersByTime(inpatNum, DateUtil.stringToDate(endTimeStr), InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue());
        //记录统计时长字段
        float dTime = 0f;
        //记录统计结束时间字段
        Date endDate = null;
        //记录统计开始时间字段
        Date sumStartDate = null;
        //若结束时间不在医嘱的计划开始时间及结束时间内则视为没有出入量医嘱不做统计
        if (currPatientOrder == null){
            return ResultGenerator.genOkResult("当前患者不存在当前类型医嘱");
        }else{
            //否则则判断传递的开始时间与医嘱的计划开始时间先后，若医嘱先计划开始早于传递开始时间则传递的开始时间作为统计开始时间，若医嘱计划开始时间晚于传递开始时间则医嘱计划开始时间作为统计开始时间；
            String orderBeginTime = currPatientOrder.get("beginTime");
            //获取医嘱开始时间毫秒数字段
            Date orderBeginDate = null;
            //获取本次统计开始时间毫秒数字段
            Date startDate = null;
            try {
                orderBeginDate = sdf.parse(orderBeginTime);
                startDate = sdf.parse(startTimeStr);
                endDate = sdf.parse(endTimeStr);
                if(orderBeginDate.getTime() <= startDate.getTime()){
                    sumStartDate = startDate;
                }else{
                    sumStartDate = orderBeginDate;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //获取统计时长分钟数
            int minuteValue = (int) ((endDate.getTime()-sumStartDate.getTime())/(1000*60));
            //获取时长小时数之后的余数
            int minutes = minuteValue%60;
            //记录时长小数部分
            float hours = 0.0f;
            //若余数小于15分钟则视为0小时
            if(minutes>0&&minutes<15){
                hours = 0.0f;
            //若余数大于15分钟，小于45分钟则视为0.5小时
            }else if(minutes >=15 &&minutes < 45){
                hours = 0.5f;
            //若余数大于45分钟则视为1小时
            }else if(minutes >= 45){
                hours = 1.0f;
            }
            //获取统计时长小时数
            dTime = minuteValue/60 + hours;
        }

        List<EmrTemplateDetailEntity> tempDetailList = emrTemplateMapper.getTempDetailByVarCode(varCode);

        //获取出入量总结模板--当做病历模板数据存储到病历表中
        String tplId = fileIndexDataElementMapper.getElementIdByvarCode(varCode);
        EmrTemplateEntity emrTemplateEntity = emrTemplateMapper.selectByPrimaryKey(tplId);
        NurseStatisticsEntity nurseStatisticsEntity = new NurseStatisticsEntity();

        Map<String,String> patientInfo = nursePatientMapper.queryPatientInfo(inpatNum);
        String deptId = patientInfo.get("dept_id");
        Map<String, String> currentUser = getCurrentUser();
        EmrFileIndexEntity emrFileIndexEntity = new EmrFileIndexEntity();
        String emrFileIndexId = UUIDUtil.randomString();
        Timestamp currentTime =new Timestamp(System.currentTimeMillis());
        emrFileIndexEntity.setGmtCreate(currentTime);
        emrFileIndexEntity.setGmtModify(currentTime);
        emrFileIndexEntity.setEmrFileId(emrFileIndexId);
        emrFileIndexEntity.setDataVersion(1);
        emrFileIndexEntity.setFileVersion("2.1");
        emrFileIndexEntity.setEmrFileName(emrTemplateEntity.getTplName());
        emrFileIndexEntity.setHospitalId(emrTemplateEntity.getHospitalId());
        emrFileIndexEntity.setEmrFileCode("0");
        emrFileIndexEntity.setOutpatNum("0");
        emrFileIndexEntity.setInpatNum(inpatNum);

        //取值可能有问题，需要检查，做完再看
        emrFileIndexEntity.setDoctorId(currentUser.get("userId") == null ? "护士id" : currentUser.get("userId"));
        emrFileIndexEntity.setDeptId(deptId);

        emrFileIndexEntity.setFileDatetime(new Timestamp(endDate.getTime()));
        emrFileIndexEntity.setPatientIdcard(patientInfo.get("idNum"));
        emrFileIndexEntity.setTplClassId("0");
        emrFileIndexEntity.setDelFlag("0");
        emrFileIndexEntity.setLockDoctorId("0");
        emrFileIndexEntity.setLockDate(currentTime);
        emrFileIndexEntity.setCommitDate(currentTime);
        emrFileIndexEntity.setCommitDoctorId("0");
        emrFileIndexEntity.setPushFlag("0");
        emrFileIndexEntity.setAuditDate1(currentTime);
        emrFileIndexEntity.setAuditDate2(currentTime);
        emrFileIndexEntity.setAuditDoctorId1("0");
        emrFileIndexEntity.setAuditDoctorId2("0");
        emrFileIndexEntity.setFileContentId("0");
        emrFileIndexEntity.setStatus("0");
        emrFileIndexEntity.setLockFlag("0");
        emrFileIndexEntity.setTplId(tplId);
        emrFileIndexEntity.setDurDisFlag("0");
        //取值可能有问题，需要检查，做完再看
        emrFileIndexEntity.setDdEmrFileId("0");
        emrFileIndexEntity.setDdDate(currentTime);
        fileIndexEntityMapper.insert(emrFileIndexEntity);

        //根据开始时间和结束时间获取该患者出入量纪录
        List<Map<String, String>> list = fileIndexDataMapper.queryStatisticsDetailDatas(inpatNum, tplId, sumStartDate, endDate);
        // 计算出结果
        float inputValue = 0.0f;
        float outputValue = 0.0f;

        if (CollectionUtils.isNotEmpty(list)) {
            for (Map<String, String> map : list) {
                if ("hlrlz".equals(map.get("varCode"))) {
                    inputValue += Float.parseFloat(map.get("dataValue"));
                } else {
                    outputValue += Float.parseFloat(map.get("dataValue"));
                }
            }
        }

        String inputValueStr = transStatisticsValue(inputValue);
        String outputValueStr = transStatisticsValue(outputValue);

        String dValue = dTime + "";
        String statisticsText;
        statisticsText = dValue + "-小" + "-时" + "-入量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;

        String statisticsId = UUIDUtil.randomString();
        nurseStatisticsEntity.setStatisticsId(statisticsId);
        nurseStatisticsEntity.setDataVersion(1);
        nurseStatisticsEntity.setDuration(dValue);
        nurseStatisticsEntity.setFlag("0");
        nurseStatisticsEntity.setFirstStatistics("0");
        nurseStatisticsEntity.setHospitalId("");
        nurseStatisticsEntity.setStatisticsType(InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue());
        nurseStatisticsEntity.setInpatNum(inpatNum);
        nurseStatisticsEntity.setStartTime(sdf.format(sumStartDate));
        nurseStatisticsEntity.setEndTime(sdf.format(endDate));
        nurseStatisticsEntity.setStatisticsText(statisticsText);
        nurseStatisticsEntity.setFileIndexdataId(emrFileIndexId);
        nurseStatisticsEntity.setTplId(tplId);
        nurseStatisticsMapper.insert(nurseStatisticsEntity);

        NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
        inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
        inStatisticsDetailEntity.setDataVersion(1);
        inStatisticsDetailEntity.setDataText("总入量");
        inStatisticsDetailEntity.setDataValue(inputValueStr);
        inStatisticsDetailEntity.setFlag("0");
        inStatisticsDetailEntity.setHospitalId("");
        inStatisticsDetailEntity.setInoutType("0");
        inStatisticsDetailEntity.setSortNo("0");
        inStatisticsDetailEntity.setStatisticsId(statisticsId);
        inStatisticsDetailEntity.setUnit("ml");
        nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

        NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
        outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
        outStatisticsDetailEntity.setDataVersion(1);
        outStatisticsDetailEntity.setDataText("总出量");
        outStatisticsDetailEntity.setDataValue(outputValueStr);
        outStatisticsDetailEntity.setFlag("0");
        outStatisticsDetailEntity.setHospitalId("");
        outStatisticsDetailEntity.setInoutType("1");
        outStatisticsDetailEntity.setSortNo("0");
        outStatisticsDetailEntity.setStatisticsId(statisticsId);
        outStatisticsDetailEntity.setUnit("ml");
        nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);
        Map<String,String> tempMap = new HashMap<>();
        for (int i = 0;i < tempDetailList.size();i++){
            EmrTemplateDetailEntity emrTemplateDetailEntity = tempDetailList.get(i);
            FileIndexDataEntity dataEntity = new FileIndexDataEntity();
            BeanUtils.copyProperties(emrTemplateDetailEntity,dataEntity);
            String newId = UUIDUtil.randomString();
            String oldId = emrTemplateDetailEntity.getTplDetailId();
            String oldParentId = emrTemplateDetailEntity.getParentTplDetailId();
            tempMap.put(oldId,newId);
            dataEntity.setDataVersion(emrTemplateDetailEntity.getDataVersion());
            dataEntity.setFileIndexDataId(newId);
            dataEntity.setGmtCreate(new Date());
            dataEntity.setGmtModify(new Date());
            dataEntity.setHospitalId(emrTemplateEntity.getHospitalId());
            dataEntity.setEmrFileId(emrFileIndexId);
            dataEntity.setCtlId(emrTemplateEntity.getCtlId());
            if(fileIndexDataElementMapper.getElementIdByvarCode("crlzjrlz").equals(emrTemplateDetailEntity.getElementId())){
                dataEntity.setDataValue(inputValueStr);
            }else if(fileIndexDataElementMapper.getElementIdByvarCode("crlzjclz").equals(emrTemplateDetailEntity.getElementId())){
                dataEntity.setDataValue(outputValueStr);
            }else if(fileIndexDataElementMapper.getElementIdByvarCode("crlzjsj").equals(emrTemplateDetailEntity.getElementId())){
                dataEntity.setDataValue(dValue);
            }
            if (tempMap.get(oldParentId) != null) {
                dataEntity.setParentFileIndexDataId(tempMap.get(oldParentId));
            }
            fileIndexDataEntityMapper.insert(dataEntity);
        }
        return ResultGenerator.genOkResult("出入量总结创建成功");
    }

    @Override
    public List<Map<String,String>> delBeforeInsert(String inpatNum, Date nodeTime) {
        //创建一个集合用来存放被删除的统计记录ID
        List<Map<String,String>> array = new ArrayList<>();
        // 1查找需要重新统计的记录
        List<NurseStatisticsEntity> list = emrStatisticsMapper.queryNeedUpdStatistics(inpatNum, nodeTime, null);
        // 2重新统计记录
        if (CollectionUtils.isNotEmpty(list)) {
            for (NurseStatisticsEntity entity : list) {
                // 删除原统计记录
                nurseStatisticsMapper.deleteStatistics(entity.getStatisticsId());
                // 保持统计ID不变（保证引用统计的列表记录可以正常引用统计记录），创建新的统计记录
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date nodeDate = null;
                try {
                    nodeDate = sdf.parse(entity.getEndTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Map<String,String> map = new HashMap<>();
                map.put("statisticsId",entity.getStatisticsId());
                map.put("statisticsType",entity.getStatisticsType());
                map.put("endTime",entity.getEndTime());
                map.put("fileIndexDataId",entity.getFileIndexdataId());
                map.put("tplId",entity.getTplId());
                array.add(map);
            }
        }
        return array;
    }

    @Override
    public Result doStatistics(String type, String recordTime, String inpatNum) {
        // 判断是否存在医嘱
        Map<String, String> currPatientOrder = nurseOrdersMapper.queryCurrAboutOrder(inpatNum, DateUtil.stringToDate(recordTime), type);
        if (currPatientOrder == null) {
            return ResultGenerator.genOkResult("当前患者不存在当前类型医嘱");
        }
        // 返回文本数据元类型节点
        EmrFileData fileData = new EmrFileData();
        fileData.setEmrFileDataList(new ArrayList<>());

        Element element = new Element();
        String elementId = null;
        //根据type获取统计种类
        //出入量总结
        if ("1".equals(type)) {
            elementId = fileIndexMapper.querySysElementId(InOutValueTypeEnum.IN_OUT_2HOURS.getVarCode());
        } else if ("2".equals(type)) {
            elementId = fileIndexMapper.querySysElementId(InOutValueTypeEnum.IN_OUT_8HOURS.getVarCode());
        } else if ("3".equals(type)) {
            elementId = fileIndexMapper.querySysElementId(InOutValueTypeEnum.OUT_8HOURS.getVarCode());
        } else if ("4".equals(type)) {
            elementId = fileIndexMapper.querySysElementId(InOutValueTypeEnum.OUT_24HOURS.getVarCode());
        } else if ("5".equals(type)) {
            elementId = fileIndexMapper.querySysElementId(InOutValueTypeEnum.URINE_OUT_8HOURS.getVarCode());
        } else if ("6".equals(type)) {
            elementId = fileIndexMapper.querySysElementId(InOutValueTypeEnum.URINE_OUT_24HOURS.getVarCode());
        } else if ("7".equals(type)) {
            elementId = fileIndexMapper.querySysElementId(InOutValueTypeEnum.WASH_IN_OUT_8HOURS.getVarCode());
        } else if ("8".equals(type)) {
            elementId = fileIndexMapper.querySysElementId(InOutValueTypeEnum.WASH_IN_OUT_24HOURS.getVarCode());
        }

        if (org.apache.commons.lang3.StringUtils.isBlank(elementId)) {
            return ResultGenerator.genOkResult("未找到数据元");
        } else {
            ElementEntity elementEntity = fileIndexDataElementMapper.queryElementById(elementId);
            element.setId(elementId);
            element.setType(elementEntity.getElementDataTypeId());
            element.setName(elementEntity.getElementName());
        }
        fileData.setElement(element);

        FileIndexDataEntity fileIndexDataEntity = new FileIndexDataEntity();
        fileIndexDataEntity.setDataValue("【记" + InOutValueTypeEnum.fromValue(type).getEnumName() + "】");
        fileIndexDataEntity.setShowIdentification("0");
        fileIndexDataEntity.setDeleteIdentification("0");
        fileIndexDataEntity.setPrintIdentification("0");
        fileIndexDataEntity.setElementId(elementId);
        fileData.setFileIndexDataEntity(fileIndexDataEntity);
        return ResultGenerator.genOkResult(fileData);
    }

    @Override
    public String inputOutputStatistics(String inpatNum, String recordTime, String statisticsType, String statisticsId, String fileIndexDataId,String tplId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date nodeDate = null;
        try {
            nodeDate = sdf.parse(recordTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long t0 = System.currentTimeMillis();
        NurseIntokeStokeTime nurseIntokeStokeTime = computeInputOutputTime(inpatNum, nodeDate, statisticsType);
        long t1 = System.currentTimeMillis();
        logger.info("获取统计时间范围耗时： {} ms", t1 - t0);

        if (nurseIntokeStokeTime == null) {
            return null;
        }

        NurseStatisticsEntity nurseStatisticsEntity = new NurseStatisticsEntity();
        nurseStatisticsEntity.setStatisticsId(statisticsId == null ? UUIDUtil.randomString() : statisticsId);
        nurseStatisticsEntity.setDataVersion(1);
        nurseStatisticsEntity.setDuration(nurseIntokeStokeTime.getdValue());
        nurseStatisticsEntity.setFlag("0");
        nurseStatisticsEntity.setFirstStatistics(nurseIntokeStokeTime.getFirst());
        nurseStatisticsEntity.setHospitalId("");
        nurseStatisticsEntity.setStatisticsType(statisticsType);
        nurseStatisticsEntity.setInpatNum(inpatNum);
        nurseStatisticsEntity.setStartTime(sdf.format(nurseIntokeStokeTime.getBeginDate()));
        nurseStatisticsEntity.setEndTime(sdf.format(nurseIntokeStokeTime.getEndDate()));
        nurseStatisticsEntity.setFileIndexdataId(fileIndexDataId);
        nurseStatisticsEntity.setTplId(tplId);

        //获取统计明细表中的相关数据返回list
        List<Map<String, String>> list = fileIndexDataMapper.queryStatisticsDetailDatas(inpatNum, statisticsType, nurseIntokeStokeTime.getBeginDate(), nurseIntokeStokeTime.getEndDate());
        //将明细表中的数据拼接为字符串放入statistics表中的statisticsText字段
        String statisticsText = countStatistics(nurseStatisticsEntity.getStatisticsId(), inpatNum, list, nurseIntokeStokeTime, statisticsType);

        nurseStatisticsEntity.setStatisticsText(statisticsText);
        // 插入新的统计记录
        nurseStatisticsMapper.insert(nurseStatisticsEntity);
        return nurseStatisticsEntity.getStatisticsId();
    }

    /**
     * 计算获取统计起始时间和统计时长
     */
    private NurseIntokeStokeTime computeInputOutputTime(String inpatNum, Date nodeTime, String statisticsType) {
        NurseIntokeStokeTime nurseIntokeStokeTime = new NurseIntokeStokeTime();

        // 1.获取当前病人最近两条医嘱，如果最近一条的开始时间和上一条的结束时间分钟级别相同，
        // 那么以上一条的医嘱记录作为起始时间，当前时间 - 医嘱时间 = 统计时长
        // 先查询两条医嘱，如果两条医嘱都已经停止
        // 查询最近两条医嘱
        // 先获取该患者最近一条未停止医嘱
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date startDate = new Date();
        Map<String, String> currPatientOrder = nurseOrdersMapper.queryCurrAboutOrder(inpatNum, nodeTime, statisticsType);
        if (currPatientOrder != null) {
            try {
                startDate = sdf.parse(currPatientOrder.get("beginTime"));
                // 获取该患者与最近一条未停止医嘱上一条医嘱并且上一条停止时间和最近一条的开立时间相同的记录，如果有，把上一条医嘱的开立时间作为起始时间
                String beforeOrder = nurseOrdersMapper.queryLastAboutOrder(inpatNum, startDate, statisticsType, currPatientOrder.get("ordersItem"));
                if (beforeOrder != null) {
                    String beginDate = beforeOrder;
                    do {
                        beginDate = nurseOrdersMapper.queryLastAboutOrder(inpatNum, sdf.parse(beginDate), statisticsType, currPatientOrder.get("ordersItem"));
                        if (beginDate!=null) {
                            beforeOrder = beginDate;
                        }
                    } while (beginDate!=null);

                    startDate = sdf.parse(beforeOrder);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            return null;
        }

        // 用来标识是否是医嘱后第一次记录 0第一次，1非第一次
        String first = "0";

        // 1、查询出入量统计表当前患者最近一次2小时出入量记录
        NurseStatisticsEntity nurseStatisticsEntity = nurseStatisticsMapper.queryLastStatisticsEntity(inpatNum, statisticsType, nodeTime);

        if (nurseStatisticsEntity != null) {

            Date lastRecordDate = new Date();
            try {
                lastRecordDate = sdf.parse(nurseStatisticsEntity.getEndTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            // 比较最近记录与医嘱，将最近时间作为起始时间
            if (lastRecordDate.getTime() > startDate.getTime()) {
                // 重新根据新的起止时间计算时长
                first = "1";
                nurseIntokeStokeTime = nurseStatisticsToPatientDateDvalue(lastRecordDate, nodeTime, first);
                logger.info("获取的最近记录日期：{}", nurseStatisticsEntity.getEndTime());
            } else {
                first = "0";
                nurseIntokeStokeTime = nurseStatisticsToPatientDateDvalue(startDate, nodeTime, first);
            }
        } else {
            first = "0";
            nurseIntokeStokeTime = nurseStatisticsToPatientDateDvalue(startDate, nodeTime, first);
        }

        return nurseIntokeStokeTime;
    }

    // 计算出入量总结并返回节点
    private String countStatistics(String statisticsId, String inpatNum, List<Map<String, String>> list, NurseIntokeStokeTime nurseIntokeStokeTime, String statisticsType) {

        // 计算出结果
        if (statisticsType.equals(InOutValueTypeEnum.IN_OUT_2HOURS.getEnumValue())) {
            // 2小时出入量计算
            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String inMiddleText = "";
            String outMiddleText = "";
            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "2";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue()));
                //dValue = nurseIntokeStokeTime.getdValue();
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisInMap = countStatisticsInput(inpatNum, list);

                statictisOutMap = countStatisticsOutput(inpatNum, list);

                // 插入入量统计详情
                Integer sortNo = 0;
                for (String key : statictisInMap.keySet()) {
                    if (statictisInMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisInMap.get(key);
                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText(key);
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisInMap.get("其他") != null && !statictisInMap.get("其他").equals(0.0f)) {
                    float value = statictisInMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                //statisticsText = "患者"+dValue+"小时入量"+totalInValue+"ml，"+qizhong+inMiddleText;
                statisticsText = "" + dValue + "小时入量" + transStatisticsValue(totalInValue) + "ml，" + qizhong + inMiddleText;

                // 插入出量统计详情
                qizhong = "";
                sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                statisticsText = statisticsText + dValue + "小时出量" + transStatisticsValue(totalOutValue) + "ml，" + qizhong + outMiddleText;
                statisticsText = statisticsText.substring(0, statisticsText.length());  //-1
                statisticsText += "。";
                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {

                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // 第一次统计 患者2小时入量**ml，其中进食**ml，白蛋白**ml，血浆**ml……余为补液，2小时出量**m l
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";
                    statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";

                } else {
                    //statisticsText = "患者2小时入量0ml，2小时出量0ml。";
                    statisticsText = "2小时入量0ml，2小时出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.IN_OUT_8HOURS.getEnumValue())) {
            // 本班出入量计算
            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String inMiddleText = "";
            String outMiddleText = "";
            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "本班";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                // dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
                // dValue = nurseIntokeStokeTime.getdValue() + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisInMap = countStatisticsInput(inpatNum, list);
                statictisOutMap = countStatisticsOutput(inpatNum, list);

                // 插入入量统计详情
                Integer sortNo = 0;
                for (String key : statictisInMap.keySet()) {
                    if (statictisInMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisInMap.get(key);
                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText(key);
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisInMap.get("其他") != null && !statictisInMap.get("其他").equals(0.0f)) {
                    float value = statictisInMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                //statisticsText = "患者"+dValue+"入量"+totalInValue+"ml，"+qizhong+inMiddleText;
                statisticsText = "" + dValue + "入量" + transStatisticsValue(totalInValue) + "ml，" + qizhong + inMiddleText;

                // 插入出量统计详情
                qizhong = "";
                sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                statisticsText = statisticsText + dValue + "出量" + transStatisticsValue(totalOutValue) + "ml，" + qizhong + outMiddleText;
                statisticsText = statisticsText.substring(0, statisticsText.length()); //-1
                statisticsText += "。";
                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // 第一次统计 患者本班入量**ml，其中进食**ml，白蛋白**ml，血浆**ml……余为补液，本班出量**m l
                    // statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";
                    //statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时入量0ml，" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时出量0ml。";
                    statisticsText = "" + dValue + "入量0ml，" + dValue + "出量0ml。";
                } else {
                    //statisticsText = "患者本班入量0ml，本班出量0ml。";
                    statisticsText = "本班入量0ml，本班出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.OUT_8HOURS.getEnumValue())) {
            // 本班出量计算
//            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
//            String inMiddleText = "";
            String outMiddleText = "";
//            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "本班";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                //dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue()))+ "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
//                statictisInMap = countStatisticsInput(inpatNum, list);
                statictisOutMap = countStatisticsOutput(inpatNum, list);

                // 插入出量统计详情
                qizhong = "";

                Integer sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                // statisticsText = "患者" + dValue+"出量"+totalOutValue+"ml，"+qizhong+outMiddleText + "。";
                statisticsText = "" + dValue + "出量" + transStatisticsValue(totalOutValue) + "ml，" + qizhong + outMiddleText + "。";

                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";
                    //statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时出量0ml。";
                    statisticsText = "" + dValue + "出量0ml。";
                } else {
                    //statisticsText = "患者本班出量0ml。";
                    statisticsText = "本班出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.OUT_24HOURS.getEnumValue())) {
            // 24小时出量计算
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String outMiddleText = "";
            float totalOutValue = 0;
            String dValue = "24小时";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
//                statictisInMap = countStatisticsInput(inpatNum, list);
                statictisOutMap = countStatisticsOutput(inpatNum, list);

                // 插入出量统计详情
                qizhong = "";

                Integer sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                // statisticsText = "患者" + dValue+"出量"+totalOutValue+"ml，"+qizhong+outMiddleText + "。";
                // statisticsText = "" + dValue+"出量"+ transStatisticsValue(totalOutValue) +"ml，"+qizhong+outMiddleText + "。";
                statisticsText = "" + dValue + "出量" + transStatisticsValue(totalOutValue) + "ml。";

                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时出量0ml。";
                    statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时出量0ml。";
                } else {
                    //statisticsText = "患者24小时出量0ml。";
                    statisticsText = "24小时出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.URINE_OUT_8HOURS.getEnumValue())) {
            // 本班尿量
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String outMiddleText = "";
            float totalOutValue = 0;
            String dValue = "本班";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                //dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisOutMap = countStatisticsUrineOutput(inpatNum, list);

                // 插入出量统计详情
                qizhong = "";

                Integer sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                //statisticsText = "患者" + dValue+"尿量"+totalOutValue+"ml，"+qizhong+outMiddleText + "。";
                statisticsText = "" + dValue + "尿量" + transStatisticsValue(totalOutValue) + "ml。"; //+qizhong+outMiddleText + "。";

                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时尿量0ml。";
                    //statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时尿量0ml。";
                    statisticsText = "" + dValue + "尿量0ml。";
                } else {
                    // statisticsText = "患者本班尿量0ml。";
                    statisticsText = "本班尿量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.URINE_OUT_24HOURS.getEnumValue())) {
            // 24小时尿量 [24小时出量计算]
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String outMiddleText = "";
            float totalOutValue = 0;
            String dValue = "24";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisOutMap = countStatisticsUrineOutput(inpatNum, list);

                // 插入出量统计详情
                qizhong = "";

                Integer sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml";
                }

                //statisticsText = "患者" + dValue+"小时尿量"+totalOutValue+"ml，"+qizhong+outMiddleText + "。";
                statisticsText = "" + dValue + "小时尿量" + transStatisticsValue(totalOutValue) + "ml。"; //+qizhong+outMiddleText + "。";

                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {
                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时尿量0ml。";
                    statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时尿量0ml。";
                } else {
                    //statisticsText = "患者24小时尿量0ml。";
                    statisticsText = "24小时尿量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.WASH_IN_OUT_8HOURS.getEnumValue())) {
            // 本班冲洗出入量计算
            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String inMiddleText = "";
            String outMiddleText = "";
            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "本班";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                //dValue = nurseIntokeStokeTime.getdValue() + "小时";
                //dValue =  transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisInMap = countStatisticsWashInOutIntake(inpatNum, list);
                statictisOutMap = countStatisticsWashInOutOutput(inpatNum, list);

                // 插入入量统计详情
                Integer sortNo = 0;
                for (String key : statictisInMap.keySet()) {
                    if (statictisInMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisInMap.get(key);
                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText(key);
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisInMap.get("其他") != null && !statictisInMap.get("其他").equals(0.0f)) {
                    float value = statictisInMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                // statisticsText = "患者"+dValue+"冲洗入量"+totalInValue+"ml，"; //+qizhong+inMiddleText;
                statisticsText = "" + dValue + "冲洗入量" + transStatisticsValue(totalInValue) + "ml，"; //+qizhong+inMiddleText;

                // 插入出量统计详情
                qizhong = "";
                sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                //statisticsText = statisticsText +dValue+"冲洗出量"+transStatisticsValue(totalOutValue)+"ml，";//+qizhong+outMiddleText;
                statisticsText = statisticsText + "冲洗出量" + transStatisticsValue(totalOutValue) + "ml，";
                statisticsText = statisticsText.substring(0, statisticsText.length() - 1);

                if (totalInValue > totalOutValue) {
                    // statisticsText += statisticsText + "，其中入超" + (totalInValue-totalOutValue) + "ml";
                    statisticsText = statisticsText + "，入超" + transStatisticsValue((totalInValue - totalOutValue)) + "ml";
                } else if (totalInValue < totalOutValue) {
                    // statisticsText += statisticsText + "，其中出超" + (totalOutValue-totalInValue) + "ml";
                    statisticsText = statisticsText + "，出超" + transStatisticsValue((totalOutValue - totalInValue)) + "ml";
                }

                statisticsText += "。";
                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {

                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // 第一次统计 患者本班入量**ml，其中进食**ml，白蛋白**ml，血浆**ml……余为补液，本班出量**m l
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时冲洗入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时冲洗出量0ml。";
                    //statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时冲洗入量0ml，" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时冲洗出量0ml。";
                    statisticsText = "" + dValue + "冲洗入量0ml，" + dValue + "冲洗出量0ml。";
                } else {
                    // statisticsText = "患者本班冲洗入量0ml，本班冲洗出量0ml。";
                    statisticsText = "本班冲洗入量0ml，本班冲洗出量0ml。";
                }
                return statisticsText;
            }
        } else if (statisticsType.equals(InOutValueTypeEnum.WASH_IN_OUT_24HOURS.getEnumValue())) {
            // 24小时冲洗出入量
            Map<String, Float> statictisInMap = new HashMap<>();
            Map<String, Float> statictisOutMap = new HashMap<>();

            String statisticsText = "";
            String inMiddleText = "";
            String outMiddleText = "";
            float totalInValue = 0;
            float totalOutValue = 0;
            String dValue = "24小时";
            if (nurseIntokeStokeTime.getFirst().equals("0")) { // 第一次统计
                // dValue = nurseIntokeStokeTime.getdValue() + "小时";
                dValue = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时";
            }
            String qizhong = "";

            if (list != null && list.size() > 0) {
                statictisInMap = countStatisticsWashInOutIntake(inpatNum, list);
                statictisOutMap = countStatisticsWashInOutOutput(inpatNum, list);

                // 插入入量统计详情
                Integer sortNo = 0;
                for (String key : statictisInMap.keySet()) {
                    if (statictisInMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    //qizhong = "其中";

                    float value = statictisInMap.get(key);
                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText(key);
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisInMap.get("其他") != null && !statictisInMap.get("其他").equals(0.0f)) {
                    float value = statictisInMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("0");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalInValue += value;

                    inMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                // statisticsText = "患者"+dValue+"冲洗入量"+totalInValue+"ml，"+qizhong+inMiddleText;
                statisticsText = "" + dValue + "冲洗入量" + transStatisticsValue(totalInValue) + "ml，"; //+qizhong+inMiddleText;


                // 插入出量统计详情
                qizhong = "";
                sortNo = 0;
                for (String key : statictisOutMap.keySet()) {
                    if (statictisOutMap.get(key).equals(0.0f) || key.equals("其他")) {
                        continue;
                    }
                    //qizhong = "其中";

                    float value = statictisOutMap.get(key);

                    NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    outStatisticsDetailEntity.setDataVersion(1);
                    outStatisticsDetailEntity.setDataText(key);
                    outStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    outStatisticsDetailEntity.setFlag("0");
                    outStatisticsDetailEntity.setHospitalId("");
                    outStatisticsDetailEntity.setInoutType("1");
                    outStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    outStatisticsDetailEntity.setStatisticsId(statisticsId);
                    outStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += key + transStatisticsValue(value) + "ml，";
                }
                // 包含其他并且值不为0.0f
                if (statictisOutMap.get("其他") != null && !statictisOutMap.get("其他").equals(0.0f)) {
                    float value = statictisOutMap.get("其他");

                    NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
                    inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
                    inStatisticsDetailEntity.setDataVersion(1);
                    inStatisticsDetailEntity.setDataText("其他");
                    inStatisticsDetailEntity.setDataValue(String.valueOf(value));
                    inStatisticsDetailEntity.setFlag("0");
                    inStatisticsDetailEntity.setHospitalId("");
                    inStatisticsDetailEntity.setInoutType("1");
                    inStatisticsDetailEntity.setSortNo(String.valueOf(sortNo++));
                    inStatisticsDetailEntity.setStatisticsId(statisticsId);
                    inStatisticsDetailEntity.setUnit("ml");
                    nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

                    totalOutValue += value;

                    outMiddleText += "其他" + transStatisticsValue(value) + "ml，";
                }

                //statisticsText = statisticsText +dValue+"冲洗出量"+transStatisticsValue(totalOutValue)+"ml，"; //+qizhong+outMiddleText;
                statisticsText = statisticsText + "冲洗出量" + transStatisticsValue(totalOutValue) + "ml，";
                statisticsText = statisticsText.substring(0, statisticsText.length() - 1);

                if (totalInValue > totalOutValue) {
                    statisticsText = statisticsText + "，入超" + transStatisticsValue((totalInValue - totalOutValue)) + "ml";
                } else if (totalInValue < totalOutValue) {
                    statisticsText = statisticsText + "，出超" + transStatisticsValue((totalOutValue - totalInValue)) + "ml";
                }

                statisticsText += "。";
                statisticsText = statisticsText.replace("，。", "。");
                return statisticsText;

            } else {

                if (nurseIntokeStokeTime.getFirst().equals("0")) {
                    // 第一次统计 患者本班入量**ml，其中进食**ml，白蛋白**ml，血浆**ml……余为补液，本班出量**m l
                    //statisticsText = "患者" + nurseIntokeStokeTime.getdValue() + "小时冲洗入量0ml，" + nurseIntokeStokeTime.getdValue() + "小时冲洗出量0ml。";
                    statisticsText = "" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时冲洗入量0ml，" + transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "小时冲洗出量0ml。";
                } else {
                    //statisticsText = "患者24小时冲洗入量0ml，24小时冲洗出量0ml。";
                    statisticsText = "24小时冲洗入量0ml，24小时冲洗出量0ml。";
                }
                return statisticsText;
            }
        } else {
            return "其他统计类型";
        }

    }

    // 统计时间距离医嘱开立时间差值
    private NurseIntokeStokeTime nurseStatisticsToPatientDateDvalue(Date startTime, Date currDate, String first) {
        NurseIntokeStokeTime nurseIntokeStokeTime = new NurseIntokeStokeTime();

        long totalMinute = (currDate.getTime() - startTime.getTime()) / 1000;
        long hour = totalMinute / 60 / 60;
        long minute = totalMinute / 60 % 60;
        Float tempHour = 0.0f;
        if (minute < 15) {
            tempHour = 0.0f;
        } else if (minute >= 15 && minute <= 45) {
            tempHour = 0.5f;
        } else {
            tempHour = 1.0f;
        }

        Float totalHour = hour + tempHour;

        nurseIntokeStokeTime.setBeginDate(startTime);
        nurseIntokeStokeTime.setEndDate(currDate);
        nurseIntokeStokeTime.setFirst(first);

        nurseIntokeStokeTime.setdValue(String.valueOf(totalHour));

        return nurseIntokeStokeTime;

    }

    public String transStatisticsValue(Float value) {
        String valueStr = String.valueOf(value);
        // 判断第二位小数不为空
        Pattern pattern1 = Pattern.compile("(\\d*\\.[0-9][1-9])");
        // 判断第一位小数不为空
        Pattern pattern2 = Pattern.compile("(\\d*\\.[1-9])");
        if (pattern1.matcher(valueStr).matches()) {
            // 是两位小数
            return valueStr;
        } else if (pattern2.matcher(valueStr).matches()) {
            // 是一位小数
            String[] strings = valueStr.split("\\.");
            return strings[0] + "." + strings[1].substring(0, 1);
        } else {
            // 是整数
            String[] strings = valueStr.split("\\.");
            return strings[0];
        }

    }

    // 计算入量
    private Map<String, Float> countStatisticsInput(String inpatNum, List<Map<String, String>> list) {

        // 获取当前患者的个人出量名称
        List<Map<String, String>> outputList = getOutputList(inpatNum);

        //获取选中的医嘱补液键值
        //List<Map<String, String>>  allMapBy = nurseOrderService.getAddByMap();

        Map<String, Float> statictisInMap = new HashMap<>();
        statictisInMap.put("进食", 0.0f);
        statictisInMap.put("引流液", 0.0f);
        statictisInMap.put("补液", 0.0f);
        statictisInMap.put("其他", 0.0f);

        List fluids = new ArrayList();
        fluids.add("ivgtt");
        fluids.add("IV微量泵维持");
        fluids.add("皮内注射");
        fluids.add("H");
        fluids.add("IM");
        fluids.add("IV");
        fluids.add("ivgtt带手术室");
        fluids.add("加入ivgtt液");


        for (Map<String, String> map : list) {
            if (map.get("varCode").equals("hlrlz")) {
                String title = map.get("dataText") == null ? "" : map.get("dataText");
                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");
                boolean contain = false;
                for (Map<String, String> map2 : outputList) {
                    String key = map2.get("dataText");
                    if (title.contains(key)) {
                        Float v = statictisInMap.get("引流液");
                        v = v + Float.parseFloat(value);
                        statictisInMap.put("引流液", v);
                        contain = true;
                        break;
                    }
                }

                if (!contain) {
                    // 判断统计map是否包含获取到的dataValue
                    if (statictisInMap.get(title) != null) {
                        // 获取统计集合里已经存在的key的值
                        Float v = statictisInMap.get(title);
                        // 累加
                        v = v + Float.parseFloat(value);
                        // 重新把值存入统计集合
                        statictisInMap.put(title, v);
                    } else {
                        if (!title.equals("")) {
                            String administration = "";
                            /*String expandFileds = map.get("expandFields");
                            if (StringUtils.isNotBlank(map.get("expandFields"))) {
                                Map expandFiledsMap = JSON.parseObject(expandFileds, Map.class);
                                administration = (String) expandFiledsMap.get("administration");
                            }*/
                            administration = administration==null?"":administration;
                            // 先判断是否可以直接把intakeStroke.getDataValue()作为统计集合的key存入统计集合
                            if (title.contains("蛋白")) { // 含蛋白类型
                                // 直接获取名称为intakeStroke.getDataValue()的值
                                Float v = Float.parseFloat(value);
                                // 把值存入统计集合
                                statictisInMap.put(title, v);
                            } else if (title.contains("鼻饲")
                                    || title.contains("空肠注入")
                                    || title.contains("混服")
                                    || title.contains("口服")
                                    || title.contains("开水冲服")
                                    || title.contains("开水泡服")
                                    || title.contains("温水冲服")
                                    || title.contains("温水泡服")
                                    || title.contains("饮水")
                                    || title.contains("进食")
                                    || title.contains("肠内营养")
                                    || administration.contains("鼻饲")
                                    || administration.contains("空肠注入")
                                    || administration.contains("混服")
                                    || administration.contains("口服")
                                    || administration.contains("开水冲服")
                                    || administration.contains("开水泡服")
                                    || administration.contains("温水冲服")
                                    || administration.contains("温水泡服")
                                    || administration.contains("饮水")
                                    || administration.contains("肠内营养")
                                    || administration.contains("进食")) { // 鼻饲类型
                                // 获取统计集合里已经存在的key的值
                                Float v = statictisInMap.get("进食");
                                // 累加
                                v = v + Float.parseFloat(value);
                                // 把值存入统计集合
                                statictisInMap.put("进食", v);
                            } else if (title.contains("ivgtt")
                                    || title.contains("补液")) {
                                // 获取统计集合里已经存在的key的值
                                Float v = statictisInMap.get("补液");
                                // 累加
                                v = v + Float.parseFloat(value);
                                // 把值存入统计集合
                                statictisInMap.put("补液", v);
                            } else {
                                // 查询是否包含在血制品中，也就是intakeStroke.getTitle中是否包含血制品字典中的某种血制品，
                                // 如果包含，直接作为血制品进行存储，如果不包含则作为其他处理
                                // 获取包含当前字符的血制品
//                            List bloodList = nurseFileIndexMapper.queryBloodDictionary(intakeStroke.getTitle());
                                Map<String, String> bloodMap = nurseFileMapper.queryBloodDatasetByName(title);

                                if (bloodMap != null && bloodMap.get("elementName") != null && bloodMap.get("elementName").length() > 0) {
                                    // 如果集合中包含血制品数据元，如：“少浆血”
                                    if (statictisInMap.containsKey(bloodMap.get("elementName"))) {
                                        // 获取统计集合里已经存在的key的值
                                        Float v = statictisInMap.get(bloodMap.get("elementName"));
                                        // 累加
                                        v = v + Float.parseFloat(value);
                                        // 把值存入统计集合
                                        statictisInMap.put(bloodMap.get("elementName"), v);
                                    } else { // 如果集合中不包含该类型，则重新设置初始值
                                        Float v = Float.parseFloat(value);
                                        // 把值存入统计集合
                                        statictisInMap.put(bloodMap.get("elementName"), v);
                                    }

                                } else {

                                    if (StringUtils.isNotBlank(administration) && fluids.contains(administration)) {
                                        Float v = statictisInMap.get("补液");
                                        v = v + Float.parseFloat(value); // 累加
                                        statictisInMap.put("补液", v);

                                    } else {
                                        Float v = statictisInMap.get("其他");
                                        v = v + Float.parseFloat(value);  //  把值存入统计集合
                                        statictisInMap.put("其他", v);
                                    }

                                }
                            }
                        } else {
                            Float v = statictisInMap.get("其他");
                            v = v + Float.parseFloat(value);
                            // 把值存入统计集合
                            statictisInMap.put("其他", v);
                        }
                    }
                }

            }
        }
        return statictisInMap;
    }

    // 计算出量
    private Map<String, Float> countStatisticsOutput(String inpatNum, List<Map<String, String>> list) {
        // 计算出结果
        // 获取当前患者的个人出量名称
        List<Map<String, String>> outputList = getOutputList(inpatNum);

        // 所有出量类型统计集合
        Map<String, Float> statictisOutMap = new HashMap<>();
        statictisOutMap.put("大便", 0.0f);
        statictisOutMap.put("尿", 0.0f);
        statictisOutMap.put("胃内容物", 0.0f);
        statictisOutMap.put("呕血", 0.0f);
        statictisOutMap.put("呕吐物", 0.0f);
        statictisOutMap.put("引流液", 0.0f);

        statictisOutMap.put("恶露", 0.0f);
        statictisOutMap.put("阴道出血", 0.0f);
        statictisOutMap.put("泌乳", 0.0f);

        statictisOutMap.put("其他", 0.0f);

        for (Map<String, String> map : list) {
            if (map.get("varCode").equals("hlclz")) {
                String title = map.get("dataText") == null ? "" : map.get("dataText");
                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");

                // 先过滤尿
                if (title.contains("尿")
                        || title.contains("膀胱造瘘引流")
                        || title.contains("膀胱造瘘管")
                        || title.contains("肾造瘘管")
                        || title.contains("肾盂造瘘管")) {
                    Float v = statictisOutMap.get("尿");
                    v = v + Float.parseFloat(value);
                    statictisOutMap.put("尿", v);
                    continue;
                }

                boolean contain = false;
                for (Map<String, String> map2 : outputList) {
                    String key = map2.get("dataText");
                    if (title.contains(key)) {
                        Float v = statictisOutMap.get("引流液");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("引流液", v);
                        contain = true;
                        break;
                    }
                }

                // contain == false不包含在引流管内
                if (contain == false) {

                    if (title.contains("大便")) {
                        Float v = statictisOutMap.get("大便");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("大便", v);
                    } else if (title.contains("胃内容物")) {
                        Float v = statictisOutMap.get("胃内容物");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("胃内容物", v);
                    } else if (title.contains("呕血")) {
                        Float v = statictisOutMap.get("呕血");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("呕血", v);
                    } else if (title.contains("呕吐物")) {
                        Float v = statictisOutMap.get("呕吐物");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("呕吐物", v);
                    } else if (title.contains("恶露")) {
                        Float v = statictisOutMap.get("恶露");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("恶露", v);
                    } else if (title.contains("阴道出血")) {
                        Float v = statictisOutMap.get("阴道出血");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("阴道出血", v);
                    } else if (title.contains("泌乳")) {
                        Float v = statictisOutMap.get("泌乳");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("泌乳", v);
                    } else {
                        Float v = statictisOutMap.get("其他");
                        v = v + Float.parseFloat(value);
                        statictisOutMap.put("其他", v);
                    }
                }

            }
        }
        return statictisOutMap;
    }


    // 计算尿量
    private Map<String, Float> countStatisticsUrineOutput(String inpatNum, List<Map<String, String>> list) {
        // 所有出量类型统计集合
        Map<String, Float> statictisMap = new HashMap<>();
        Float urine = list.stream().filter(map -> "hlclz".equals(map.get("varCode"))).filter(map -> {
            String title = map.get("dataText") == null ? "" : map.get("dataText");
            return (title.contains("尿")
                    || title.contains("膀胱造瘘引流")
                    || title.contains("膀胱造瘘管")
                    || title.contains("肾造瘘管")
                    || title.contains("肾盂造瘘管"));
        }).map(map -> map.get("dataValue") == null ? 0.0F : Float.parseFloat(map.get("dataValue"))).reduce(Float::sum).orElseGet(() -> 0.0F);
        statictisMap.put("尿", urine);
//        statictisMap.put("尿", 0.0F);
//        for (Map<String, String> map : list) {
//            if (map.get("varCode").equals("hlclz")) {
//                String title = map.get("dataText") == null ? "" : map.get("dataText");
//                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");
//
//                if (title.contains("尿")
//                        || title.contains("膀胱造瘘引流")
//                        || title.contains("膀胱造瘘管")
//                        || title.contains("肾造瘘管")
//                        || title.contains("肾盂造瘘管")) {
//                    Float v = statictisMap.get("尿");
//                    v = v + Float.parseFloat(value);
//                    statictisMap.put("尿", v);
//                }
//            }
//        }
        return statictisMap;
    }

    // 计算冲洗出入量
    private Map<String, Float> countStatisticsWashInOutIntake(String inpatNum, List<Map<String, String>> list) {

        Map<String, Float> statictisInMap = new HashMap<>();
        statictisInMap.put("冲洗入量", 0.0f);

        for (Map<String, String> map : list) {
            if (map.get("varCode").equals("hlrlz")) {
                String title = map.get("dataText") == null ? "" : map.get("dataText");
                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");

                if (title.contains("冲洗")) {
                    Float v = statictisInMap.get("冲洗入量");
                    v = v + Float.parseFloat(value);
                    // 把值存入统计集合
                    statictisInMap.put("冲洗入量", v);
                }

            }
        }

        return statictisInMap;
    }

    // 计算冲洗出入量
    private Map<String, Float> countStatisticsWashInOutOutput(String inpatNum, List<Map<String, String>> list) {

        // 所有出量类型统计集合
        Map<String, Float> statictisOutMap = new HashMap<>();
        statictisOutMap.put("冲洗出量", 0.0f);

        for (Map<String, String> map : list) {
            if (map.get("varCode").equals("hlclz")) {
                String title = map.get("dataText") == null ? "" : map.get("dataText");
                String value = map.get("dataValue") == null ? "0" : map.get("dataValue");

                if (title.contains("双套管")) {
                    Float v = statictisOutMap.get("冲洗出量");
                    v = v + Float.parseFloat(value);
                    // 把值存入统计集合
                    statictisOutMap.put("冲洗出量", v);
                }

            }

        }

        return statictisOutMap;
    }

    /**
     * 获取文书编号对应的出量名称及其扩展
     *
     * @return
     */
    private List<Map<String, String>> getOutputList(String inpatNum) {
        String longNumStr = "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿";
        // 获取当前患者的个人出量名称
        List<Map<String, String>> outputList;
        outputList = nurseFileMapper.queryOutputElementSelections();
        /*if (NurseFileTypeEnum.Care.getEnumValue().equals(recordType)) {
            outputList = nurseFileMapper.queryOutputElementSelections();
        } else {
            //新生儿、儿科导管备注获取
            outputList = nurseFileMapper.queryCommonDictionryValues("ck_jlxsedgzd");
        }*/
        // 1获取EMR_FILE_INDEX中记录的NURSE_CUSTOMIZE_EXPAND获取出量名称和项目名称的扩展记录
        /*String recordJson = nurseFileMapper.queryExtendsByInpatNum(inpatNum, recordType);
        if (recordJson != null) {
            Map<String, List<Map<String, String>>> map1 = JSON.parseObject(recordJson, Map.class);
            int optionsSrcCount = outputList.size();
            // 出量管道
            List<Map<String, String>> pipeList = new ArrayList<>();
            if (map1.get("pipe") != null && NurseFileTypeEnum.Care.getEnumValue().equals(recordType)) {
                pipeList = map1.get("pipe");
            }else if(map1.get("newpipe") != null){
                pipeList = map1.get("newpipe");
            }
            for (int i = 0; i < pipeList.size(); i++) {
                Map<String, String> map3 = pipeList.get(i);
                String currName = map3.get("name");
                String numStr = longNumStr.substring(optionsSrcCount + i, optionsSrcCount + i + 1);
                String fullName = numStr + currName;
                Map<String, String> map2 = new HashMap<>();
                map2.put("dataText", fullName);
                map2.put("id", "");
                map2.put("dataValue", "");
                outputList.add(map2);
            }
        }*/
        return outputList;
    }

    @Override
    public String inputOutputSummary(String inpatNum, Date nodeTime, String statisticsId) {
        // 时间为空返回空
        if (nodeTime == null) {
            return null;
        }
        // sumDay暂时不用，后续用来传递选择进行总结的日期
        // 获取当前7点时间
//        // 获取固定的7点时刻，用于作为统计节点和判断获取7点之前的最近一条医嘱
//        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.HOUR_OF_DAY, 17);//控制时
//        cal.set(Calendar.MINUTE, 0);//控制分
//        cal.set(Calendar.SECOND, 0);//控制秒
//        Date today7Clock = cal.getTime();

        // 根据时间获取未停的需要24小时总结的医嘱
        // 获取7点之前最近一条统计记录
        long t0 = System.currentTimeMillis();
        NurseIntokeStokeTime nurseIntokeStokeTime = computeInputOutputTime(inpatNum, nodeTime, InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue());
        long t1 = System.currentTimeMillis();
        logger.info("获取统计时间范围耗时： {} ms", t1 - t0);

        if (nurseIntokeStokeTime == null) {
            return null;
        }

        // 创建统计记录
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        NurseStatisticsEntity nurseStatisticsEntity = new NurseStatisticsEntity();
        nurseStatisticsEntity.setStatisticsId(statisticsId == null ? UUIDUtil.randomString() : statisticsId);
        nurseStatisticsEntity.setDataVersion(1);
        nurseStatisticsEntity.setDuration(nurseIntokeStokeTime.getdValue());
        nurseStatisticsEntity.setFlag("0");
        nurseStatisticsEntity.setFirstStatistics(nurseIntokeStokeTime.getFirst());
        nurseStatisticsEntity.setHospitalId("");
        nurseStatisticsEntity.setStatisticsType(InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue());
        nurseStatisticsEntity.setInpatNum(inpatNum);
        nurseStatisticsEntity.setStartTime(sdf.format(nurseIntokeStokeTime.getBeginDate()));
        nurseStatisticsEntity.setEndTime(sdf.format(nurseIntokeStokeTime.getEndDate()));

        List<Map<String, String>> list = fileIndexDataMapper.queryStatisticsDetailDatas(inpatNum, InOutValueTypeEnum.IN_OUT_SUMMARY.getEnumValue(), nurseIntokeStokeTime.getBeginDate(), nurseIntokeStokeTime.getEndDate());

        String statisticsText = countStatisticsSummary(nurseStatisticsEntity.getStatisticsId(), inpatNum, list, nurseIntokeStokeTime);
        nurseStatisticsEntity.setStatisticsText(statisticsText);
        // 插入新的统计记录
        nurseStatisticsMapper.insert(nurseStatisticsEntity);
        return nurseStatisticsEntity.getStatisticsId();
    }

    // 计算出入量总结并返回节点
    private String countStatisticsSummary(String statisticsId, String inpatNum, List<Map<String, String>> list, NurseIntokeStokeTime nurseIntokeStokeTime) {
        // 计算出结果
        float inputValue = 0.0f;
        float outputValue = 0.0f;

        if (list != null && list.size() > 0) {
            for (Map<String, String> map : list) {
                if (map.get("varCode").equals("hlrlz")) {
                    inputValue += Float.parseFloat(map.get("dataValue"));
                } else {
                    outputValue += Float.parseFloat(map.get("dataValue"));
                }
            }
        }

        String inputValueStr = transStatisticsValue(inputValue);
        String outputValueStr = transStatisticsValue(outputValue);

        //入量计算
        NurseStatisticsDetailEntity inStatisticsDetailEntity = new NurseStatisticsDetailEntity();
        inStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
        inStatisticsDetailEntity.setDataVersion(1);
        inStatisticsDetailEntity.setDataText("总入量");
        inStatisticsDetailEntity.setDataValue(inputValueStr);
        inStatisticsDetailEntity.setFlag("0");
        inStatisticsDetailEntity.setHospitalId("");
        inStatisticsDetailEntity.setInoutType("0");
        inStatisticsDetailEntity.setSortNo("0");
        inStatisticsDetailEntity.setStatisticsId(statisticsId);
        inStatisticsDetailEntity.setUnit("ml");
        nurseStatisticsDetailMapper.insert(inStatisticsDetailEntity);

        NurseStatisticsDetailEntity outStatisticsDetailEntity = new NurseStatisticsDetailEntity();
        outStatisticsDetailEntity.setDetailId(UUIDUtil.randomString());
        outStatisticsDetailEntity.setDataVersion(1);
        outStatisticsDetailEntity.setDataText("总出量");
        outStatisticsDetailEntity.setDataValue(outputValueStr);
        outStatisticsDetailEntity.setFlag("0");
        outStatisticsDetailEntity.setHospitalId("");
        outStatisticsDetailEntity.setInoutType("1");
        outStatisticsDetailEntity.setSortNo("0");
        outStatisticsDetailEntity.setStatisticsId(statisticsId);
        outStatisticsDetailEntity.setUnit("ml");
        nurseStatisticsDetailMapper.insert(outStatisticsDetailEntity);

        String statisticsText = "";
        if (nurseIntokeStokeTime.getFirst().equals("0")) {
            // 第一次统计
            statisticsText = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "-小" + "-时" + "-入量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;
        } else {
            statisticsText = "24" + "-小" + "-时" + "-入量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;
        }
        return statisticsText;

    }

    public Map<String, String> getCurrentUser() {
        String userId = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
                .getHeader("zuul_id");
        Map<String, String> currentUser = fileIndexMapper.getUser(userId);
        if (currentUser == null)
            return new HashMap<String, String>();
        return currentUser;
    }

    private String countFileIndexDataSummary( List<Map<String, String>> list, NurseIntokeStokeTime nurseIntokeStokeTime) {
        // 计算出结果
        float inputValue = 0.0f;
        float outputValue = 0.0f;

        if (list != null && list.size() > 0) {
            for (Map<String, String> map : list) {
                if (map.get("varCode").equals("hlrlz")) {
                    inputValue += Float.parseFloat(map.get("dataValue"));
                } else {
                    outputValue += Float.parseFloat(map.get("dataValue"));
                }
            }
        }

        String inputValueStr = transStatisticsValue(inputValue);
        String outputValueStr = transStatisticsValue(outputValue);

        String statisticsText = "";
        if (nurseIntokeStokeTime.getFirst().equals("0")) {
            // 第一次统计
            statisticsText = transStatisticsValue(Float.parseFloat(nurseIntokeStokeTime.getdValue())) + "-小" + "-时" + "-入量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;
        } else {
            statisticsText = "24" + "-小" + "-时" + "-入量" + "-总" + "-结" + "-" + inputValueStr + "-出" + "-量" + "-总" + "-结" + "-" + outputValueStr;
        }
        return statisticsText;

    }
}
